﻿//======================================================================
// 枚举的扩展功能类
//====================================================================== 

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Data;

namespace Common
{
    /// <summary>
    /// 枚举的扩展功能类
    /// </summary>
    public sealed class EnumPlus
    {
        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(Enum e)
        {
            FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] EnumAttributes
                = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return e.ToString();

        }

        #endregion

        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, string enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                if (e.ToString() == enumValue)
                {
                    result = GetEnumDesc(e);
                    break;
                }
            }

            return result;

        }

        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, byte enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                try
                {
                    if (Convert.ToInt32(e) == enumValue)
                    {
                        result = GetEnumDesc(e);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog("枚举转换异常", ex);
                    break;
                }
            }

            return result;

        }

        #endregion

        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, int enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                try
                {
                    if (Convert.ToInt32(e) == enumValue)
                    {
                        result = GetEnumDesc(e);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog("枚举转换异常", ex);
                    break;
                }
            }

            return result;

        }

        #endregion


        #region 将含有描述信息的枚举绑定到列表控件中

        /// <summary>
        /// 将含有描述信息的枚举绑定到列表控件中
        /// </summary>
        /// <param name="listControl"></param>
        /// <param name="enumType"></param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType)
        {
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
            }
        }

        /// <summary>
        /// 重载将含有描述信息的枚举绑定到列表控件中的方法
        /// </summary>
        /// <param name="listControl">控件名字</param>
        /// <param name="enumType">枚举类型申明</param>
        /// <param name="isInt">需要给Value什么值，True的话为常量值数字(int类型)，False为常量值名称(英文string类型)</param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType, bool isInt)
        {
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                if (isInt)
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
                else
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), enumValue.ToString()));
            }
        }

        /// <summary>
        /// 重载将含有描述信息的枚举绑定到列表控件中的方法
        /// </summary>
        /// <param name="listControl">控件名字</param>
        /// <param name="enumType">枚举类型申明</param>
        /// <param name="isInt">需要给Value什么值，True的话为常量值数字(int类型)，False为常量值名称(英文string类型)</param>
        /// <param name="listItem">自定义ListItem</param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType, bool isInt, ListItem listItem)
        {
            listControl.Items.Add(listItem);
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                if (isInt)
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
                else
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), enumValue.ToString()));
            }
        }

        #endregion

        #region 将含有描述信息的枚举绑定到字典中
        /// <summary>
        /// 将含有描述信息的枚举绑定到字典中
        /// </summary>
        /// <param name="enumType">枚举类别</param>
        /// <returns>返回一个包含Key和Value的字典</returns>
        public static Dictionary<string, string> GetEnumKey(System.Type enumType)
        {
            Dictionary<string, string> SMPramKey = new Dictionary<string, string>();
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                SMPramKey.Add(GetEnumDesc(e), enumValue.ToString());
            }
            return SMPramKey;
        }
        /// <summary>
        /// 将含有描述信息的枚举绑定到字典中
        /// </summary>
        /// <param name="listControl"></param>
        /// <param name="enumType"></param>
        public static Dictionary<string, int> GetEnumKeyInt(System.Type enumType)
        {
            Dictionary<string, int> SMPramKey = new Dictionary<string, int>();
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                try
                {
                    SMPramKey.Add(GetEnumDesc(e), Convert.ToInt32(enumValue));
                }
                catch (Exception)
                {
                    SMPramKey.Add(GetEnumDesc(e), 0);
                }
            }
            return SMPramKey;
        }
        #endregion

        #region 将含有描述信息的枚举组装datatable
        /// <summary>
        /// 将含有描述信息的枚举组装datatable(Table两个字段，text是描述，value是值(默认是int的值))
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static DataTable GetEnumTable(System.Type enumType)
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                dr = dt.NewRow();
                Enum e = (Enum)enumValue;
                try
                {
                    dr["Text"] = GetEnumDesc(e);
                    dr["Value"] = Convert.ToInt32(enumValue).ToString();
                    dt.Rows.Add(dr);
                }
                catch (Exception)
                {

                }
            }
            return dt;
        }
        #endregion


        #region 将枚举数据绑定到DropDownList中
        /// <summary>
        /// 将枚举数据绑定到DropDownList中
        /// </summary>
        /// <param name="ddlList">需要绑定的DropDownList</param>
        /// <param name="type">枚举</param>
        public static void GetDropdownlistBindByEnum(DropDownList ddlList, Type type)
        {
            IList<object> list = EnumHelper.GetItems(type);
            ddlList.DataSource = list;
            ddlList.DataTextField = "Text";
            ddlList.DataValueField = "Value";
            ddlList.DataBind();
            ddlList.Items.Insert(0, new ListItem("==请选择==", "-1"));
        }
        #endregion
    }
}
